//
//  CarCityNamesView.m
//  CarCare
//
//  Created by ileo on 15/2/1.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "CarCityNamesView.h"
#import "SettingMgr.h"

@interface CityNameView : UIView

@property (nonatomic, copy) NSString *name;
@property (nonatomic, strong) UILabel *nameL;
@property (nonatomic, copy) void (^Click)(NSString *name);
@property (nonatomic, assign) BOOL isSelected;

@end

@implementation CityNameView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.nameL = [Factory labelWithFrame:self.bounds font:FONT_DEFAULT_Light(22 * __MAIN_RATIO_375) text:@"" textColor:COLOR_HEXSTRING(@"#424242") onView:self textAlignment:NSTextAlignmentCenter];
        __weak __typeof(self) wself = self;
        self.layer.borderColor = [Theme lineColor].CGColor;
        self.layer.borderWidth = 0.5;
        [self resetCornerRadius:2];
        [[UIButton buttonEmptyWithFrame:self.bounds click:^{
            if (wself.Click) {
                wself.Click(wself.name);
            }
        }] setupOnView:self];
    }
    return self;
}

-(void)setName:(NSString *)name{
    _name = name;
    self.nameL.text = name;
}

-(void)setIsSelected:(BOOL)isSelected{
    _isSelected = isSelected;
    if (isSelected) {
        self.backgroundColor = COLOR_HEXSTRING(@"1f90e5");
        [self.nameL setTextColor:[UIColor whiteColor]];
    }else{
        self.backgroundColor = [UIColor clearColor];
        [self.nameL setTextColor:COLOR_HEXSTRING(@"#424242")];
    }
}

@end

#define ONE_NUM (__MAIN_WIDTH == 320 ? 7 : 8)

@interface CarCityNamesView()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, copy) NSArray *names;

@property (nonatomic, strong) CityNameView *selectedCityV;

@end

@implementation CarCityNamesView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderColor = COLOR_HEXSTRING(@"#cbcbcb").CGColor;
        self.layer.borderWidth = 1;
        
        self.scrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        [self addSubview:self.scrollView];
        
        NSArray *arr = SETTINGMGR.carCityNames;
        NSInteger all = arr.count;
        CGFloat viewLeft = 14 * __MAIN_RATIO_375;
        CGFloat viewXandY = 10 * __MAIN_RATIO_375;
        CGFloat  width = (CGRectGetWidth(frame) - viewLeft * 2 - viewXandY * (ONE_NUM - 1))/ONE_NUM ,height = width;
        CGFloat viewTop = 22 * __MAIN_RATIO_375;
        CGFloat viewBottom = 19 * __MAIN_RATIO_375;
        CGFloat x = viewLeft , y = viewTop;
        CGFloat maxY = 0;
        for (int i = 0; i < all; i++) {
            CityNameView *view = [[CityNameView alloc] initWithFrame:CGRectMake(x, y, width, height)];
            [self.scrollView addSubview:view];
            view.name = arr[i];
            __weak __typeof(view) wView = view;
            view.Click = ^(NSString *name){
                self.selectedCityV.isSelected = NO;
                if (self.Choice) {
                    self.Choice(name);
                }
                self.selectedCityV = wView;
                self.selectedCityV.isSelected = YES;
            };
            if ((!self.selectedCityV) && [view.name isEqualToString:@"沪"]) {
                self.selectedCityV = view;
                self.selectedCityV.isSelected = YES;
            }
            x += width + viewXandY;
            
            if ((i+1)%ONE_NUM == 0) {
                x = viewLeft;
                y += height + viewXandY;
            }
            if (view.bottom > maxY) {
                maxY = view.bottom;
            }
        }
        self.scrollView.contentSize = CGSizeMake(CGRectGetWidth(frame), maxY + viewBottom);
        self.scrollView.height = maxY + viewBottom;
        self.height = maxY + viewBottom;

        
    }
    return self;
}

@end
